package com.ruoyi.loan.service.impl;

import com.ruoyi.loan.constant.ApproveType;
import com.ruoyi.loan.constant.LoanRecordStatus;
import com.ruoyi.loan.entity.LoanRecord;
import com.ruoyi.loan.entity.User;
import com.ruoyi.loan.mapper.LoanRecordMapper;
import com.ruoyi.loan.service.LoanService;
import com.ruoyi.loan.service.UserService;
import com.ruoyi.loan.utils.FileUtils;
import com.ruoyi.loan.utils.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * Author: 廖振辉
 * Date: 2022/4/5
 * Description:
 */
@Service
public class LoanServiceImpl implements LoanService {

    @Autowired
    private UserService userService;

    @Autowired
    private LoanRecordMapper loanRecordMapper;

    @Autowired
    private FileUtils fileUtils;

    @Override
    public void remindRepayment(Integer userId) {
        // 查询用户邮箱，发送催款邮件
        User user = userService.getById(userId);
        if (user == null) {
            return;
        }
        String mail = user.getMail();
        Boolean flag = MailUtils.sendEmail(mail
                , "资助管理系统提醒您，您有助学贷款记录未还，请注意贷款期限，详情请登录资助管理系统进行查询，若您已还款，可忽略该条提醒！"
                , "资助管理系统提醒测试");
        if (!flag) {
            throw new RuntimeException("用户名为" + user.getUsername() + "邮箱为：" + mail + "，的用户邮件发送失败");
        }
    }

    @Override
    public void uploadVoice(MultipartFile file, Integer loanId) {
        checkFile(file);
        LoanRecord loanRecord = loanRecordMapper.selectById(loanId);
        if (loanRecord == null){
            throw new RuntimeException("不存在的贷款记录");
        }
        if (loanRecord.getStatus() == null || loanRecord.getStatus() > LoanRecordStatus.VISITED){
            throw new RuntimeException("已完成审批，无法再次回访");
        }
        // 文件保存
        String filePath = FileUtils.upLoadFile(file, FileUtils.LOAN_VOICE);
        loanRecord.setVisitVoicePath(filePath);
        String showFilePath = fileUtils.getShowFilePath(filePath);
        System.out.println(showFilePath);
        loanRecord.setStatus(LoanRecordStatus.VISITED);
        loanRecordMapper.updateById(loanRecord);
    }

    @Override
    public void approveLoanApply(Integer adminId, Integer loanId, String refuseReasons,Integer approve) {
        LoanRecord loanRecord = loanRecordMapper.selectById(loanId);
        if (loanRecord == null){
            throw new RuntimeException("不存在的贷款记录");
        }
        if (loanRecord.getStatus() == null || loanRecord.getStatus() < LoanRecordStatus.VISITED){
            throw new RuntimeException("审批前需要先上传回访录音");
        }
        if (ApproveType.APPROVE.equals(approve)){
            // 同意
            loanRecord.setStatus(LoanRecordStatus.APPROVED);
        }else {
            // 拒绝
            loanRecord.setStatus(LoanRecordStatus.REFUSED);
            loanRecord.setRefuseReasons(refuseReasons);
        }
        loanRecord.setAdminTime(new Date());
        loanRecord.setAdminId(adminId);
        loanRecordMapper.updateById(loanRecord);
    }

    private void checkFile(MultipartFile file) {
        // 最大100M
        long maxFileSize = 1024 * 1024 * 100;
        if (file.getSize() > maxFileSize){
            throw new RuntimeException("文件超过最大上传大小，100M");
        }
        String filename = file.getOriginalFilename();
        if (filename == null){
            throw new RuntimeException("请上传允许的文件类型(mp3，wma)");
        }
        if (!filename.endsWith("mp3") && !filename.endsWith("wma")){
            throw new RuntimeException("请上传允许的文件类型(mp3，wma)");
        }
    }
}
